/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agenda.contatos.bsi4;

import agenda.contatos.bsi4.database.Conexao;
import agenda.contatos.bsi4.database.ContatoModel;
import com.mysql.jdbc.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;

/**
 *
 * @author uli
 */
public class frmAgenda extends javax.swing.JFrame {
    private DefaultListModel listaContatos = new DefaultListModel();

    /**
     * Creates new form frmAgenda
     */
    public frmAgenda() throws SQLException {
        initComponents();
        setListagem();
    }
    
    private void setListagem() throws SQLException {
        Connection con = Conexao.getConexao();
        
         try {
            Statement stm = con.createStatement();
            ContatoModel contatoModel = new ContatoModel();
            Contato[] listagemContatos = contatoModel.listarContatos(stm);

            for(int i = 0; i <= listagemContatos.length - 1; i++) {
                listaContatos.addElement(listagemContatos[i].getNome());
            }

            con.close();
        } catch (SQLException e) {
            System.out.println(e.getErrorCode());
        }
        
        pnlContatos.setModel(listaContatos);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        painel = new javax.swing.JScrollPane();
        pnlContatos = new javax.swing.JList();
        lblNome = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        lblEndereco = new javax.swing.JLabel();
        txtEndereco = new javax.swing.JTextField();
        lblTelefonePrincipal = new javax.swing.JLabel();
        txtDddP = new javax.swing.JTextField();
        txtTelefonePrincipal = new javax.swing.JTextField();
        lblTelefoneSecundario = new javax.swing.JLabel();
        txtDddS = new javax.swing.JTextField();
        txtTelefoneSecundario = new javax.swing.JTextField();
        btnSalvar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pnlContatos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        pnlContatos.setToolTipText("");
        painel.setViewportView(pnlContatos);

        lblNome.setText("Nome");

        lblEndereco.setText("Endereço");

        lblTelefonePrincipal.setText("Telefone principal");

        txtDddP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDddPActionPerformed(evt);
            }
        });

        txtTelefonePrincipal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTelefonePrincipalActionPerformed(evt);
            }
        });

        lblTelefoneSecundario.setText("Telefone secundario");

        txtDddS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDddSActionPerformed(evt);
            }
        });

        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(lblNome)
                                .addComponent(txtNome)
                                .addComponent(lblEndereco)
                                .addComponent(txtEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(51, 51, 51)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(txtDddP, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txtTelefonePrincipal))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(txtDddS, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txtTelefoneSecundario, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(0, 0, Short.MAX_VALUE))
                                .addComponent(lblTelefoneSecundario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnSalvar))
                    .addComponent(lblTelefonePrincipal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(painel, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblNome)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblEndereco)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lblTelefonePrincipal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtDddP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTelefonePrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblTelefoneSecundario)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtDddS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtTelefoneSecundario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                        .addComponent(btnSalvar))
                    .addComponent(painel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtDddPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDddPActionPerformed
        
    }//GEN-LAST:event_txtDddPActionPerformed

    private void txtTelefonePrincipalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTelefonePrincipalActionPerformed
        
    }//GEN-LAST:event_txtTelefonePrincipalActionPerformed

    private void txtDddSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDddSActionPerformed
        
    }//GEN-LAST:event_txtDddSActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        if (! txtNome.getText().equals("")) {
            Contato novoContato = new Contato();
            Connection con = Conexao.getConexao();
            
            novoContato.setNome(txtNome.getText());
        
            if (! txtDddP.getText().equals("") && ! txtTelefonePrincipal.getText().equals("")) {
                novoContato.setDdd(0, txtDddP.getText());
                novoContato.setTelefone(0, txtTelefonePrincipal.getText());
            }

            if (! txtDddS.getText().equals("") && ! txtTelefoneSecundario.getText().equals("")) {
                novoContato.setDdd(1, txtDddS.getText());
                novoContato.setTelefone(1, txtTelefoneSecundario.getText());
            }
            
            if (! txtEndereco.getText().equals("")) {
                novoContato.setEndereco(txtEndereco.getText());
            }
            
            try {
                Statement stm = con.createStatement();
                ContatoModel contatoModel = new ContatoModel();

                contatoModel.inserirPessoa(stm, novoContato);
                
                this.listaContatos.addElement(novoContato.getNome() + " " + novoContato.getTelefone(0) + " " + novoContato.getTelefone(1));
                
                con.close();
            } catch (SQLException e) {
                System.out.println(e.getErrorCode());
            }
        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmAgenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmAgenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmAgenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmAgenda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new frmAgenda().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(frmAgenda.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSalvar;
    private javax.swing.JLabel lblEndereco;
    private javax.swing.JLabel lblNome;
    private javax.swing.JLabel lblTelefonePrincipal;
    private javax.swing.JLabel lblTelefoneSecundario;
    private javax.swing.JScrollPane painel;
    private javax.swing.JList pnlContatos;
    private javax.swing.JTextField txtDddP;
    private javax.swing.JTextField txtDddS;
    private javax.swing.JTextField txtEndereco;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextField txtTelefonePrincipal;
    private javax.swing.JTextField txtTelefoneSecundario;
    // End of variables declaration//GEN-END:variables
}
